/*
 * @Description  : 
 * @Author       : BuGua
 * @Date         : 2021-08-09 16:29:34
 * @LastEditors  : BuGua
 * @LastEditTime : 2021-08-11 15:49:49
 */
import { Effect, Reducer, history } from 'umi';
import { message } from 'antd';
import { queryLogin, getSmscode, addUser, selectUser, getUserInfo, delUser, updateUser } from '@/services/login';

import { ConnectState, LoginUserInfoState } from './connect.d';
export interface LoginModelState {
  userInfo: LoginUserInfoState;
  isError: boolean;
}

export interface LoginModelType {
  namespace: 'login';
  state: LoginModelState;
  effects: {
    getUserInfo: Effect;
    queryLogin: Effect;
    getSmscode: Effect;
  };
  reducers: {
    save: Reducer<LoginModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<LoginModelState>;
  };
}

const LoginModel: LoginModelType = {
  namespace: 'login',
  state: {
    userInfo: {
      user_id: 0,
      iconurl: '',
      nickname: '',
      mobile: '',
    },
    isError: false,
  },
  effects: {
    //登录
    *queryLogin({ payload }, { call, put }):any {
      // const { name } = yield select((state: ConnectState) => state.global);
      console.log(payload,'payload');
      
      const response = yield call(queryLogin, { ...payload });
      if (response && response.code == 200) {
        const { token, userInfo } = response.data;
        localStorage.userInfo = JSON.stringify(userInfo);
        localStorage.token = token
        sessionStorage.setItem('Authorization', "token")
        message.success('登录成功！');
        history.replace('/');
      } else {
        response.message && message.error(response.message)
      }
    },
    *getUserInfo({ payload }, { call, put, select }):any {
      const { name } = yield select((state: ConnectState) => state.global);
      const data = yield call(queryLogin, { ...payload, name });
      yield put({
        type: 'save',
        payload: {
          userInfo: data,
        },
      });
    },
    //添加
    *addUser({ payload }:any, { call, put, select }:any) {
      const res = yield call(addUser, { ...payload });
      return res
    },
    //删除
    *delUser({ payload }, { call, put, select }):any {
      const res = yield call(delUser, { ...payload });
      return res
    },
    //修改
    *updateUser({ payload }, { call, put, select }) {
      const res = yield call(updateUser, { ...payload });
      return res
    },
    //查询用户信息
    *selectUser({ payload }, { call, put, select }) {
      const res = yield call(selectUser, { ...payload });
      return res
    },

    *getSmscode(_, { call }) {
      // const response = yield call(getSmscode);
      // if (response.status === 'ok') {
      localStorage.removeItem('userInfo');
      localStorage.removeItem('token');
      localStorage.removeItem('Authorization');
      history.replace({
        pathname: '/login',
        search: `timestamp=${new Date().getTime()}`,
      });
      // }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
};

export default LoginModel;
